{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时延数据文件处理\n",
    "import json\n",
    "filepath='D:\\\\Program_Files\\\\Geth\\\\YanMengAudit\\\\shard\\\\finalRes\\\\'\n",
    "fnlist=['A-CLIENTaudit-bft2-pop_kad20-kadvary3-curve3-100-0.0-encode8-2-6-654400-byzantine2.txt',\n",
    "    'A-CLIENTaudit-bft2-pop_kad20-kadvary3-curve3-100-0.0-encode8-2-6-654400-byzantine1.txt',\n",
    "    'A-CLIENTaudit-bft2-pop_kad20-kadvary3-curve3-100-0.0-encode8-2-6-654400-crash1.txt',\n",
    "    'A-CLIENTaudit-bft2-pop_kad20-kadvary3-curve3-100-0.0-encode8-2-6-654400-byc2.txt',\n",
    "    'A-CLIENTaudit-bft2-pop_kad20-kadvary3-curve3-100-0.0-encode8-2-6-654400-crash2.txt',\n",
    "    'A-CLIENTaudit-piecekad3-pop_kad20-kadvary3-curve3-100-0.0-encode4-2-6-654400-byzantine1.txt',\n",
    "    'A-CLIENTaudit-piecekad3-pop_kad20-kadvary3-curve3-100-0.0-encode4-2-6-654400-byzantine2.txt',\n",
    "    'A-CLIENTaudit-piecekad3-pop_kad20-kadvary3-curve3-100-0.0-encode4-2-6-654400-byc2.txt',\n",
    "    'A-CLIENTaudit-piecekad3-pop_kad20-kadvary3-curve3-100-0.0-encode4-2-6-654400-crash1.txt',\n",
    "    'A-CLIENTaudit-piecekad3-pop_kad20-kadvary3-curve3-100-0.0-encode4-2-6-654400-crash2.txt'\n",
    "    ]\n",
    "for filename in fnlist:\n",
    "    avg_time=[]\n",
    "    avg_access=[]\n",
    "    avg_timec=[]\n",
    "    avg_accessc=[]\n",
    "    i=-1\n",
    "    with open(filepath+filename,'r') as fr:\n",
    "        for lines in fr:\n",
    "            i+=1\n",
    "            if i==0:\n",
    "                pass\n",
    "            elif i==1:\n",
    "                avg_time=json.loads(lines)\n",
    "            elif i==2:\n",
    "                avg_access=json.loads(lines)\n",
    "            elif i==3:\n",
    "                avg_timec=json.loads(lines)\n",
    "            elif i==4:\n",
    "                avg_accessc=json.loads(lines)\n",
    "    # 分别处理没有经过解码的请求时延和经过编码的请求时延\n",
    "    outputfilename='output-'+filename\n",
    "    with open (filepath+outputfilename,'w') as fw:\n",
    "        for i in range(len(avg_time)):\n",
    "            print(str(avg_time[i]/avg_access[i])+' '+str(avg_timec[i]/avg_accessc[i])+' '+str((avg_time[i]+avg_timec[i])/(avg_access[i]+avg_accessc[i])),file=fw)\n",
    "    request_times_fn='out-times'+filename\n",
    "    with open(filepath+request_times_fn,'w')as fw:\n",
    "        for i in range(len(avg_access)):\n",
    "            print(str(avg_accessc[i]/(avg_access[i]+avg_accessc[i])*100)+'%',file=fw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时延文件的特殊处理，因为100次不能保证每一个epoch都有数据，因此综合了多个文件，注意，同时处理不解码和解码时延\n",
    "import json\n",
    "import numpy as np\n",
    "# oldtimes=[100,101,99,50,51,49,24,26,76]## 共576，极限了，我不会再加了\n",
    "# oldtimes=[100,101,99,50,74,76,51,49]# for crash\n",
    "oldtimes=[100,199,201]# for byc\n",
    "filepath='D:\\\\WorkSpaceTmp\\\\BSS\\\\AnalysisSanning\\\\finalTest\\\\finalRes\\\\'\n",
    "fnpattern1='A-CLIENTzipf-piecekad3-pop_kad10-kadvary3-curve3-'\n",
    "fnpattern2='-0.0-encode4-2-6-654400-byc2.txt'#byzantine2 #crash2 #byc2\n",
    "fnlist=[fnpattern1+str(ot)+fnpattern2 for ot in oldtimes]\n",
    "avg_time=[]\n",
    "avg_access=[]\n",
    "avg_timec=[]\n",
    "avg_accessc=[]\n",
    "for findex in range(len(oldtimes)):\n",
    "    i=-1\n",
    "    with open(filepath+fnlist[findex],'r') as fr:\n",
    "        for lines in fr:\n",
    "            i+=1\n",
    "            if i==0:\n",
    "                pass\n",
    "            elif i==1:\n",
    "                if findex==0:\n",
    "                    avg_time=np.array(json.loads(lines))\n",
    "                else:\n",
    "                    avg_time=avg_time+np.array(json.loads(lines))\n",
    "            elif i==2:\n",
    "                if findex==0:\n",
    "                    avg_access=np.array(json.loads(lines))\n",
    "                else:\n",
    "                    avg_access=avg_access+np.array(json.loads(lines))\n",
    "            elif i==3:\n",
    "                if findex==0:\n",
    "                    avg_timec=np.array(json.loads(lines))\n",
    "                else:\n",
    "                    avg_timec=avg_timec+np.array(json.loads(lines))\n",
    "            elif i==4:\n",
    "                if findex==0:\n",
    "                    avg_accessc=np.array(json.loads(lines))\n",
    "                else:\n",
    "                    avg_accessc=avg_accessc+np.array(json.loads(lines))\n",
    "    # 分别处理没有经过解码的请求时延和经过编码的请求时延\n",
    "outputfilename='output-'+fnpattern1+str(sum(oldtimes))+fnpattern2\n",
    "with open (filepath+outputfilename,'w') as fw:\n",
    "    for i in range(len(avg_time)):\n",
    "        print(str(avg_time[i]/avg_access[i])+' '+str(avg_timec[i]/avg_accessc[i])+' '+str((avg_time[i]+avg_timec[i])/(avg_access[i]+avg_accessc[i])),file=fw)\n",
    "\n",
    "request_times_fn='out-times'+fnpattern1+str(sum(oldtimes))+fnpattern2\n",
    "with open(filepath+request_times_fn,'w')as fw:\n",
    "    for i in range(len(avg_access)):\n",
    "        print(str(avg_accessc[i]/(avg_access[i]+avg_accessc[i])*100)+'%',file=fw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 存储需求文件处理\n",
    "import numpy as np\n",
    "import json\n",
    "filepath='D:\\\\Program_Files\\\\Geth\\\\YanMengAudit\\\\shard\\\\finalRes\\\\'\n",
    "fnlist=[\n",
    "    'S-CLIENTaudit-piecekad3-pop_kad10-kadvary3-curve3-100-0.0-encode4-2-6-654400-byzantine1.txt',\n",
    "    'S-CLIENTaudit-piecekad3-pop_kad10-kadvary3-curve3-100-0.0-encode4-2-6-654400-byzantine2.txt',\n",
    "    'S-CLIENTaudit-piecekad3-pop_kad10-kadvary3-curve3-100-0.0-encode4-2-6-654400-byc2.txt',\n",
    "    'S-CLIENTaudit-piecekad3-pop_kad10-kadvary3-curve3-100-0.0-encode4-2-6-654400-crash1.txt',\n",
    "    'S-CLIENTaudit-piecekad3-pop_kad10-kadvary3-curve3-100-0.0-encode4-2-6-654400-crash2.txt'\n",
    "    ]\n",
    "nodes_sum=10\n",
    "storages=[]\n",
    "onereplica=[]\n",
    "i=0\n",
    "for filename in fnlist:\n",
    "    with open(filepath+filename,'r') as fr:\n",
    "        for lines in fr:\n",
    "            if i==0:\n",
    "                i+=1\n",
    "                continue\n",
    "            elif i==1:\n",
    "                onereplica=np.array(json.loads(lines))\n",
    "                i+=1\n",
    "            elif (i<12):# 不处理后面对块数统计的数据，只处理具体的MB数据大小\n",
    "                i+=1\n",
    "                if not len(storages):\n",
    "                    storages=np.array(json.loads(lines))\n",
    "                else:\n",
    "                    storage_=json.loads(lines)\n",
    "                    storages=storages+np.array(storage_)\n",
    "    # storages=storages/nodes_sum\n",
    "    storages=storages/onereplica\n",
    "    outputfilename='output-storage'+filename\n",
    "    with open (filepath+outputfilename,'w') as fw:\n",
    "        for data in storages:\n",
    "            print(data,file=fw)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5476\n"
     ]
    }
   ],
   "source": [
    "# 处理encode和noencode存储分析\n",
    "filepath='D:\\\\WorkSpaceTmp\\\\BSS\\\\AnalysisSanning\\\\'\n",
    "filename='tmp-encode.txt'\n",
    "passive=0\n",
    "with open(filepath+filename,'r')as fr:\n",
    "    for lines in fr:\n",
    "        values=lines.split('=')\n",
    "        if values[0]=='total':\n",
    "            passive+=int(values[1])\n",
    "print(passive)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算blocknums个区块的网络ID\n",
    "import numpy as np\n",
    "import json\n",
    "blocknums=1000\n",
    "blockIDs=np.random.choice(range(0,pow(2,16)),size=blocknums,replace=False)\n",
    "print(blockIDs.tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算constantrank，按照访问热度从低到高的顺序列出区块的编号（注意，编号从0开始，与“ID”不同）\n",
    "import numpy as np\n",
    "import json\n",
    "blocknums=1000\n",
    "blockIDs=np.random.choice(range(0,blocknums),size=blocknums,replace=False)\n",
    "print(blockIDs.tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改文件YanMengAuditRecord.txt中的块号，修改成从654000开始，和实验中的块号对应起来\n",
    "beginblk=654000\n",
    "RECORDS=[]\n",
    "with open('D:\\Program_Files\\Geth\\YanMengAudit\\YanMengAuditRecord.txt','r')as fr:\n",
    "    for lines in fr:\n",
    "        x1,x2=lines.split(';')\n",
    "        oneRecord=x1.split(',')\n",
    "        # print(oneRecord)\n",
    "        if(len(oneRecord)>1):\n",
    "            RECORDS.append([int(oneRecord[0]),oneRecord[1],oneRecord[2],oneRecord[3],int(oneRecord[4])])\n",
    "startblk=RECORDS[0][4]\n",
    "plusblk=beginblk-startblk\n",
    "for i in range(len(RECORDS)):\n",
    "    RECORDS[i][4]=RECORDS[i][4]+plusblk\n",
    "with open('D:\\Program_Files\\Geth\\YanMengAudit\\YanMengAuditRecord.txt','w')as fw:\n",
    "    for record in RECORDS:\n",
    "        line=','.join([str(xx) for xx in record])+';'\n",
    "        print(line,file=fw)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "07aa5fbee9d52421aae60b313f39256ceedc4b99267d2c7ce50cdb08ef7e2a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
